package com.juyu.miliao.scheduler.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.juyu.miliao.common.constant.RedisKeyPrefix;
import com.juyu.miliao.common.constant.SpringFestivalActivitiesGiftEnums;
import com.juyu.miliao.common.constant.SpringFestivalActivitiesRedisKey;
import com.juyu.miliao.scheduler.domin.GiftSpend;
import com.juyu.miliao.scheduler.service.SpringFestivalActivitiesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author xuanqicheng
 * @create 2019-28-16:58
 */
@Service
public class SpringFestivalActivitiesServiceImpl implements SpringFestivalActivitiesService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public static final Integer SIZE = 5;


    @Override
    public void setSpringFestivalActivitiesInfo(GiftSpend giftSpend) {

        if (giftSpend.getGiftId() ==SpringFestivalActivitiesGiftEnums.GIFT_FIRE_WORKS.getGiftId()
                || giftSpend.getGiftId()== SpringFestivalActivitiesGiftEnums.GIFT_LUCKY_BAG.getGiftId()
                || giftSpend.getGiftId() ==SpringFestivalActivitiesGiftEnums.GIFT_YUAN_BO.getGiftId()
                || giftSpend.getGiftId() ==SpringFestivalActivitiesGiftEnums.GIFT_MAN_MON.getGiftId()
                || giftSpend.getGiftId() ==SpringFestivalActivitiesGiftEnums.GIFT_GOLDEN_PIG.getGiftId()) {


            String key = SpringFestivalActivitiesRedisKey.SPRING_FESTIVAL_ACTIVITIES_USER_KEY+giftSpend.getInUserId();

            String type = SpringFestivalActivitiesGiftEnums.getTypeByGiftId(giftSpend.getGiftId());

            long number = stringRedisTemplate.opsForHash().increment(key,type,giftSpend.getNum());

            /**
             * 根据类型设置zset
             */
            String typeKey = SpringFestivalActivitiesRedisKey.SPRING_FESTIVAL_ACTIVITIES_TYPE_KEY+type;

            stringRedisTemplate.opsForZSet().add(typeKey,giftSpend.getInUserId()+"",number);



            List<Object> hashKeyList = getHashKeyList();

            List<Object> list = stringRedisTemplate.opsForHash().multiGet(key, hashKeyList);

            List<Integer> sortList = new ArrayList<>();
            if(list.size()==SIZE){
                for(Object obj : list){
                    if(obj!=null){
                        String temp = (String) obj;
                        sortList.add(Integer.parseInt(temp));
                    }
                }
                Collections.sort(sortList);
            }

            if(sortList.size()==SIZE){
                int num =sortList.get(0);
                stringRedisTemplate.opsForZSet().add(SpringFestivalActivitiesRedisKey
                        .SPRING_FESTIVAL_ACTIVITIES_GOLD_LIST_KEY,giftSpend.getInUserId()+"",num);
            }

        }
    }

    /**
     * 获取hashKey集合
     * @return
     */
    private List<Object> getHashKeyList() {
        List<Object> hashKeyList = new ArrayList<>();

        hashKeyList.add(SpringFestivalActivitiesGiftEnums.GIFT_FIRE_WORKS.getType());

        hashKeyList.add(SpringFestivalActivitiesGiftEnums.GIFT_LUCKY_BAG.getType());

        hashKeyList.add(SpringFestivalActivitiesGiftEnums.GIFT_YUAN_BO.getType());

        hashKeyList.add(SpringFestivalActivitiesGiftEnums.GIFT_MAN_MON.getType());

        hashKeyList.add(SpringFestivalActivitiesGiftEnums.GIFT_GOLDEN_PIG.getType());

        return hashKeyList;
    }



}
